{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\ProgramData\\Anaconda3\\lib\\site-packages\\h5py\\__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n",
      "  from ._conv import register_converters as _register_converters\n",
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "from functools import partial\n",
    "\n",
    "import keras\n",
    "from keras.optimizers import SGD, RMSprop\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "from keras import backend as K\n",
    "from keras.callbacks import EarlyStopping, ReduceLROnPlateau\n",
    "from keras.layers import Dense, LSTM, Bidirectional, Lambda, Input, TimeDistributed\n",
    "from keras.layers.embeddings import Embedding\n",
    "from keras.models import Sequential, Model\n",
    "from keras.utils import plot_model\n",
    "from sklearn.metrics import precision_score\n",
    "from sklearn.model_selection import train_test_split"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_precision_score(labels, predictions):\n",
    "    score = 0\n",
    "    for i in range(0, len(labels)):\n",
    "        if labels[i] in predictions[i]:\n",
    "            score += 1\n",
    "    return score / predictions.size\n",
    "def get_recall_score(labels, predictions):\n",
    "    score = 0\n",
    "    for i in range(0, len(labels)):\n",
    "        if labels[i] in predictions[i]:\n",
    "            score += 1\n",
    "    return score / len(labels)\n",
    "def create_dataset(dataset):\n",
    "    dataX, dataY = [], []\n",
    "    for line in dataset.itertuples():\n",
    "        lineindex = line[0]\n",
    "        thislist = dataset.loc[lineindex].values\n",
    "        a = thislist[:-1]\n",
    "        b = thislist[-1]\n",
    "        dataX.append(a)\n",
    "        dataY.append(b)\n",
    "    return np.array(dataX), np.array(dataY)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def def_model(w2vDimension, n_symbols, input_length):\n",
    "    model = Sequential()\n",
    "    model.add(Embedding(output_dim=w2vDimension, input_dim=n_symbols, mask_zero=False, input_length=input_length, trainable=False))\n",
    "    model.add(LSTM(hidden_dim_1, return_sequences=True))\n",
    "    model.add(Dense(nb_classes, activation='softmax'))\n",
    "    return model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dataframe = pd.read_csv('../datas/100thousand@6.csv', engine='python', sep='|', header=None)\n",
    "input_length = dataframe.shape[1] - 1\n",
    "nb_classes = dataframe.values.max() + 1\n",
    "train, validation = train_test_split(dataframe, test_size=0.2, random_state=14)\n",
    "print(len(train), len(validation))\n",
    "\n",
    "X_train, Y_train = create_dataset(train)\n",
    "X_valid, Y_valid = create_dataset(validation)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import gc\n",
    "del train, validation\n",
    "gc.collect();"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "embedding_Dimension = 100\n",
    "hidden_dim_1 = 200\n",
    "hidden_dim_2 = 100\n",
    "NUM_CLASSES = nb_classes\n",
    "\n",
    "base_network = def_model(embedding_Dimension, nb_classes, input_length)\n",
    "rcnn_model = Model(inputs=input_a, outputs=output)\n",
    "epochs = 100\n",
    "lrate = 0.01\n",
    "decay = lrate / epochs\n",
    "sgd = SGD(lr=lrate, momentum=0.9, decay=decay, nesterov=False)\n",
    "top20_acc = partial(keras.metrics.sparse_top_k_categorical_accuracy, k=20)\n",
    "top20_acc.__name__ = 'acc'\n",
    "rcnn_model.compile(loss='sparse_categorical_crossentropy', optimizer=sgd, metrics=[top20_acc])\n",
    "callbacks = [EarlyStopping(monitor='val_loss', patience=3, verbose=1)]\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
